package com.hotel.common.user.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.hotel.common.config.SystemConfig;
import com.hotel.common.param.UserVipAdd;
import com.hotel.common.user.service.VipService;
import com.hotel.common.util.JwtUtil;
import com.hotel.common.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 * vip用户表 前端控制器
 * </p>
 *
 * @author KK
 * @since 2024-06-14
 */
@RestController
@RequestMapping("/user/vip")
public class VipController {

    @Autowired
    private VipService vipService;

    @RequestMapping("/save")
    public R save(HttpServletRequest request, @RequestBody UserVipAdd userVipAdd) {

        //参数校验
        if (userVipAdd == null) {
            return R.fail("参数异常");
        }

        //解析token
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        //获取当前用户id
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        //将当前用户id赋值给userVipAdd
        userVipAdd.setUserId(id);

        //根据userVipAdd中的type属性不同，选择不同的支付方式
        switch (userVipAdd.getVipType()) {
            case 1:
                //钱包余额支付
                return vipService.walletPay(userVipAdd);
            case 2:
                //支付宝支付
                break;
            case 3:
                //微信支付
                break;

        }

        return R.fail("支付类型异常");
    }

    @RequestMapping("/my")
    public R selectVip(HttpServletRequest request) {

        String token = request.getHeader(SystemConfig.HEADER_TOKEN); // 假设SystemConfig在你的项目中已定义
        return vipService.selectVipByToken(token);

    }

}

